/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import tw.com.hitevision.whiteboard.android.Device;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.util.TouchUtils;

public class EraserMenuView extends RelativeLayout {
    private final static String TAG = EraserMenuView.class.getSimpleName();
    private ImageView partialEraser;
    private ImageView objectEraser;
    private View eventView;
    public enum EraserMode {
        NONE, OBJECT, PARTIAL
    }
    private EraserMode curMode = EraserMode.PARTIAL;
    private OnChangeListener listener;
    public EraserMenuView(Context context) {
        super(context);
        init(context);
    }

    public EraserMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EraserMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setOnChangeListener(OnChangeListener listener) {
        this.listener = listener;
    }

    @SuppressLint("ClickableViewAccessibility")
    private void init(Context context) {
        final Resources r = getResources();
        setBackgroundResource(R.drawable.menu_bubble_eraser);
        inflate(context, R.layout.menu_eraser, this);
        partialEraser = findViewById(R.id.eraser_partial);
        partialEraser.setOnTouchListener(touchListener);
//        partialEraser.setOnClickListener(v -> {
//            setEraserMode(EraserMode.PARTIAL);
//        });
//        partialEraser.setSelected(false);

        objectEraser = findViewById(R.id.eraser_object);
        objectEraser.setOnTouchListener(touchListener);
//        objectEraser.setOnClickListener(v -> {
//            setEraserMode(EraserMode.OBJECT);
//        });
    }

    private void setEraserMode(EraserMode mode){
        if(listener == null){
            Log.e(TAG,"Eraser OnChangeListener is null, can't use this feature");
            return;
        }
        curMode = mode;
        if(curMode == EraserMode.PARTIAL){
            partialEraser.setSelected(true);
            objectEraser.setSelected(false);
//            listener.onSelectEeaserProp(Device.STYLUS,partialEraser);
        }else{
            partialEraser.setSelected(false);
            objectEraser.setSelected(true);
//            listener.onSelectEeaserProp(Device.STYLUS,objectEraser);
        }
    }

    private void setEraserMode(EraserMode mode, Device device) {
        if(listener == null){
            Log.e(TAG,"Eeaser OnChangeListener is null, can't use this feature");
            return;
        }
        curMode = mode;
        if(curMode == EraserMode.PARTIAL){
            partialEraser.setSelected(true);
            objectEraser.setSelected(false);
            listener.onSelectEeaserProp(device,partialEraser);
        }else{
            partialEraser.setSelected(false);
            objectEraser.setSelected(true);
            listener.onSelectEeaserProp(device,objectEraser);
        }
    }

    public EraserMode getCurMode() {
        return curMode;
    }

    public void notifyEraserInput(){
        setEraserMode(curMode);
    }

    public void notifyEraserInput(Device device) {
        setEraserMode(curMode,device);
    }

    public void notifyEraserInput(Device device, View eventView) {
        if (listener == null)
            return;
        if (eventView.getId() == R.id.eraser_object) {
            curMode = EraserMode.OBJECT;
            listener.onSelectEeaserProp(device,objectEraser);
        } else {
            curMode = EraserMode.PARTIAL;
            listener.onSelectEeaserProp(device,partialEraser);
        }
    }

    private final OnTouchListener touchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            eventView = v;
            return gestureListener.onSingleTapUp(event);
        }
    };

    private final GestureDetector.OnGestureListener gestureListener =
            new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Device device = TouchUtils.getEventDevice(getContext(), e.getActionIndex(), e);
            if (device == Device.PALM) {
                device = Device.FINGER;
            }
            objectEraser.setSelected(false);
            partialEraser.setSelected(false);
            eventView.setSelected(true);
            notifyEraserInput(device, eventView);

            return true;
        }

    };


    public interface OnChangeListener {
        void onSelectEeaserProp(Device device, View v);
    }

}
